package com.mf.controller;

import com.mf.entity.Dictionary;
import com.mf.result.Result;
import com.mf.service.IDictionaryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
* <p>
    * 数据字典 前端控制器
    * </p>
*
* @author mf
* @since 2024-06-08
*/
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    @Resource
    private IDictionaryService dictionaryService;
    /**
     * 新增数据字典
     */
    @PostMapping("/add")
    public Result<Integer> add(@Valid @RequestBody Dictionary dictionary)
    {
        return Result.success(dictionaryService.add(dictionary));
    }

    /**
     * 数据字典删除
     */
    @GetMapping("/delete")
    public Result<Boolean> delete(@RequestParam("id")  Integer id)
    {
        return Result.success(dictionaryService.removeById(id));
    }

    /**
     * 数据字典修改
     */
    @PostMapping("/update")
    public Result<Boolean> update(@Valid @RequestBody Dictionary dictionary)
    {
        dictionary.setCode(null);
        return Result.success(dictionaryService.updateById(dictionary));
    }

    /**
     * 根据数据字典code查询对应字典
     */
    @GetMapping("/findByCode")
    public Result<List<Dictionary>> findByCode(@RequestParam("code") String code)
    {
        return Result.success(dictionaryService.findByCode(code));
    }
}
